package com.rh.controller;

import com.rh.Enums;
import com.rh.dto.Page;
import com.rh.dto.Result;
import com.rh.entity.Student;
import com.rh.excrptions.DoException;
import com.rh.service.StudentService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@CrossOrigin
@Controller
@ResponseBody

@RequiresRoles(value = {"teacher","admin"},logical = Logical.OR )
public class StudentController {
    @Autowired
    private StudentService studentService;
    /**
     * 分页数据
     */
    //认证才能访问
    @RequiresAuthentication
    @RequestMapping(value = "/getStudentPage",method = RequestMethod.GET)
    public Result getStudentPage(Integer pageNo,Integer pageSize) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 3;
            }
            Page studentPage = studentService.getStudentPage(pageNo, pageSize);
            Result result = new Result();
            result.setData(studentPage);
            return result;
        }catch (Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
    /**
     * 添加学员
     */
    @RequiresAuthentication
    @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
    public Result addStudent(Student student){
        try{
            //int i=1/0;
            studentService.addMethod(student);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.ADD_ERROR);
        }
    }
    /**
     * 删除学员
     */
    @RequiresAuthentication
    @RequestMapping(value = "/deleteStudent",method = RequestMethod.GET)
    public Result deleteStudent(Integer studentId){
        try{
            //int i=1/0;
            studentService.deleteMethod(studentId);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.DELETE_ERROR);
        }
    }
    /**
     * 修改学员信息
     */
    @RequiresAuthentication
    @RequestMapping(value = "/updateStudent",method = RequestMethod.POST)
    public Result updateStudent(Student student){
        try{
            //int i=1/0;
            studentService.updateMethod(student);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.UPDATE_ERROR);
        }
    }
    /**
     * 通过id查询学员信息
     */
    @RequiresAuthentication
    @RequestMapping(value = "/getStudentByStudentId",method = RequestMethod.GET)
    public Result getStudentByStudentId(Integer studentId) {
        try {
            Student methodAllById = studentService.getMethodAllById(studentId);
            System.out.println(methodAllById);
            Result result = new Result();
            result.setData(methodAllById);
            return result;
        }catch (Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
}
